package com.example.firstfive;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;
import android.os.Message;
import android.widget.SimpleAdapter;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

public class RateGridviewActivity extends AppCompatActivity implements
        AdapterView.OnItemClickListener  { //实现接口
    private static final String TAG = "提示";
    GridView gridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rate_gridview);
        //初始化数据项
        ArrayList<HashMap<String, String>> listItems = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < 30; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "Rate: " + i); //标题文字
            map.put("ItemDetail", "Detail: " + i); //详情描述
            listItems.add(map);

        }
        //生成适配器的Item,和动态数组对应的元素
        SimpleAdapter listItemAdapter = new SimpleAdapter(this, listItems,
                R.layout.list_item,//lISTitem的布局实现
                new String[]{"ItemTitle", "ItemDetail"},
                new int[]{R.id.title, R.id.detail});
        //绑定控件
        gridView = findViewById(R.id.gridview);
        gridView.setAdapter(listItemAdapter);
        gridView.setOnItemClickListener(this);


        Handler handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                //super.handleMessage(msg);
                //收到数据
                if (msg.what == 7) {

                    ArrayList<HashMap<String, String>> list = (ArrayList<HashMap<String, String>>) msg.obj;
                    //构造Adapter
                    SimpleAdapter listItemAdapter = new SimpleAdapter(RateGridviewActivity.this,
                            list,//listItem 数据源
                            R.layout.list_item,//lISTitem的布局实现
                            new String[]{"cname", "cval"},
                            new int[]{R.id.title, R.id.detail});
                    gridView.setAdapter(listItemAdapter);

                }
                super.handleMessage(msg);

            }
        };
        MapTask task = new MapTask();
        task.setHandler(handler);
        Thread t = new Thread(task);// 用MapTask时换成task
        t.start();
    }

    // 重写方法,点击事件处理
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "OnItemClick: ");
        //获得数据项
        HashMap<String, String> map = (HashMap<String, String>) gridView.getItemAtPosition(position);
        Log.i(TAG, "onItemClick:cname=" + map.get("cname"));
        Log.i(TAG, "onItemClick:cval=" + map.get("cval"));

        Log.i(TAG, "onItemClick: 打开新窗口");
        Intent Itemclick = new Intent(this, RateTransfActivity.class);
        String currency = map.get("cname");
        float rate = Float.parseFloat(map.get("cval"));
        Itemclick.putExtra("cur_key", currency);
        Itemclick.putExtra("rate_key", rate);

        Log.i(TAG, "openOne: cur_key=" + currency);
        Log.i(TAG, "openOne: rate_key=" + rate);

        startActivity(Itemclick);

    }
}

